<?php
/**
 * PsiFramework
 *
 * Php Simple Framework
 *
 * PHP version 5.3
 *
 * @category  PsiFramework.Plugins.Shop.Classes.DAO
 * @package   PsiFramework.Plugins.Shop.Classes.DAO
 * @author    Remi San <remi.san@gmail.com>
 * @copyright 2011 Remi San
 * @license   http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @version   SVN: <svn_id>
 * @link      http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Shop\Classes\DAO\OrderItemLinkDAO.html
 */

namespace PsiFramework\Plugins\Shop\Classes\DAO;

use PsiFramework\Classes\Factory\DAOFactory;

import("Classes.DAO.AbstractDAO");
import("Classes.Util.Parameters.ObjectParameters");

use \PsiFramework\Classes\DAO\AbstractDAO;
use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Util\Parameters\ObjectParameters;


/**
 * The OrderItemLink Data Access Object
 *
 * This is the default class for the OrderItemLink Data Access Object
 *
 * @category PsiFramework.Plugins.Shop.Classes.DAO
 * @package  PsiFramework.Plugins.Shop.Classes.DAO
 * @author   Remi San <remi.san@gmail.com>
 * @license  http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @link     http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Shop\Classes\DAO\OrderItemLinkDAO.html
 */
class OrderItemLinkDAO extends AbstractDAO
{
    /**
     * The singleton
     * 
     * @var OrderItemLinkDAO
     */
    protected static     $_singleton = null;

    /**
     * OrderItemLinkDAO constructor
     * 
     * @param ObjectParameters $objectParameters The object parameters
     * 
     * @return OrderItemLinkDAO
     */
    protected function __construct(
        ObjectParameters $objectParameters=null
    )
    {
        parent::__construct($objectParameters);
    }
    
    /**
     * Delete
     * 
     * @param Object &$object            The object to delete
     * @param bool   $performTransaction Does the delete action
     *                                    need a transaction ?
     * @param bool   $checkSecurity      Does the delete action
     *                                    need to check the security ?
     * 
     * @see PsiFramework\Classes\DAO.AbstractDAO::delete()
     * 
     * @return boolean
     */
    public function delete(
		Object   &$object,
		/*bool*/ $performTransaction=true,
		/*bool*/ $checkSecurity=true
    )
    {
        $ok = true ;
        if ($performTransaction) {
        	$this->_database->startTransaction();
        }
        
        $linkTaxDao = DAOFactory::getDAO('OrderItemLinkTax');
        foreach ($object->getTaxes() as $taxLink) {
            $taxLink->setLink($object);
            $ok = $linkTaxDao->delete($taxLink, false, $checkSecurity);
            if (!$ok) {
                break;
            }
        }
        
        if ($ok) {
            $ok = parent::delete($object, false, $checkSecurity);
        }
        
        if ($performTransaction) {
        	if ($ok) {
        		$this->_database->commit();
        	} else {
        		$this->_database->rollback();
        	}
        }
        
        return $ok;
    }
    
    /**
     * The function to call instead of the constructor.
     *
     *@param ObjectParameters $objectParameters the object parameters 
     *                                          for OrderItem
     *
     * @return OrderItemLinkDAO instance
     */
    public static function getInstance(ObjectParameters $objectParameters)
    {

        if (self::$_singleton==null) {
            self::$_singleton = new OrderItemLinkDAO($objectParameters);
        }

        return self::$_singleton;
    }
}
